package jwt

import (
	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt/v5"
	"time"
)

// SignatureKey JWT签名key
const (
	// SignatureKey JWT签名Key
	SignatureKey = "UcDe5rrlQ8q1QuPmxVI3O3c6YIgEwYdY"

	// TokenHeaderKey JWT Token Header Key
	TokenHeaderKey = "X-TOKEN"

	//UserAgentHeaderKey User-Agent Header Key
	UserAgentHeaderKey = "User-Agent"

	// ExpireTime Token过期时间
	ExpireTime = time.Minute * 30
)

// UserClaims JWT用户登录信息
type UserClaims struct {
	jwt.RegisteredClaims        //组合jwt.RegisteredClaims
	Uid                  int64  //用户ID
	UserAgent            string //User-Agent
}

// GenerateLoginToken 生成登录Token
func GenerateLoginToken(ctx *gin.Context, uid int64) (string, error) {
	uc := UserClaims{
		Uid:       uid,
		UserAgent: ctx.GetHeader(UserAgentHeaderKey), //将浏览器的UserAgent作为登录的辅助信息,也生成到Token中,用于登录时的身份校验
		RegisteredClaims: jwt.RegisteredClaims{
			ExpiresAt: jwt.NewNumericDate(time.Now().Add(ExpireTime)), //设置过期时间
		},
	}
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, uc)
	return token.SignedString([]byte(SignatureKey))
}
